package com.example.chiro;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Table_general {

	public static final String ROW_ID = "_id";
	public static final String DATE = "date";
	public static final String NOM = "nom";
	public static final String PRENOM = "prenom";
	public static final String LONGITUDE = "longitude";
	public static final String LATITUDE = "latitude";
	public static final String ALTITUDE = "altitude";
	public static final String COMMUNE = "commune";
	public static final String LIEU_DIT = "lieu_dit";
	public static final String ENVOYE = "envoye";

	public static final String DATABASE_TABLE = "general";
	private DataBaseHelper DBHelper;
	private SQLiteDatabase DB;
	private final Context context;

	private static class DataBaseHelper extends SQLiteOpenHelper {
		public DataBaseHelper(Context context) {
			super(context, DB_FICHEARBRE_adapter.DATABASE_NAME, null,
					DB_FICHEARBRE_adapter.DATABASE_VERSION);
		}

		public void onCreate(SQLiteDatabase arg0) {
		}

		public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		}
	}

	public Table_general(Context c) {
		this.context = c;
	}

	public Table_general open() throws SQLException {
		this.DBHelper = new DataBaseHelper(this.context);
		this.DB = this.DBHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		this.DBHelper.close();
	}

	// créé une nouvelle ligne, retourne l'id si reussit, et sinon -1
	public long inserer_unparun(String date, String nom, String prenom,
			String longitude, String latitude, String altitude, String commune,
			String lieu_dit) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(DATE, date);
		initialValues.put(NOM, nom);
		initialValues.put(PRENOM, prenom);
		initialValues.put(LONGITUDE, longitude);
		initialValues.put(LATITUDE, latitude);
		initialValues.put(ALTITUDE, altitude);
		initialValues.put(COMMUNE, commune);
		initialValues.put(LIEU_DIT, lieu_dit);
		return this.DB.insert(DATABASE_TABLE, null, initialValues);
	}

	public long inserer_parSlide(ContentValues content) {
		return this.DB.insert(DATABASE_TABLE, null, content);
	}

	// supprime une ligne, renvoie vrai si OK, faux sinon
	public boolean delete_donnees_general(long rowId) {
		return this.DB.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0;
	}

	public int compter_nombre_fiche_totale() {
		int c = DB.query(DATABASE_TABLE, new String[] { "_id" }, null, null,
				null, null, null).getCount();
		return c;
	}
	
	public int nombre_non_envoye() {
		int c = DB.query(DATABASE_TABLE, new String[] { "_id" }, ENVOYE + "=" + "false", null,
				null, null, null).getCount();
		return c;
	}

	public Cursor getAll() {
		Cursor resultat = null;
		//resultat = this.DB.query(DATABASE_TABLE, new String[]{DATE,NOM,PRENOM,LONGITUDE,LATITUDE,ALTITUDE,COMMUNE,LIEU_DIT,ENVOYE}, ENVOYE+"=\"false\"", null, null, null, null, null);
		resultat = this.DB.query(DATABASE_TABLE, new String[]{DATE,NOM,PRENOM,LONGITUDE,LATITUDE,ALTITUDE,COMMUNE,LIEU_DIT,ENVOYE}, null, null, null, null, null, null);
		return resultat;
	}
	
	public boolean update_envoi(int rowId, String etat) {
	    ContentValues args = new ContentValues();
	    args.put(ENVOYE, etat);
	    return this.DB.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) > 0;
	}
	
}
